package com.yjs.app.trade.transaction.service;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yjs.app.trade.transaction.util.PropertiesUtils;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

/**
 * @Description: 极光推送服务
 * @author: Tandong
 * @date: 2017年12月22日 下午3:18:29
 * @Copyright: 2017 重庆网达科技 All rights reserved.
 */
public class JPushService {

	private static final String APP_KEY = PropertiesUtils.getProperty("jpush_app_key");
	private static final String SECRET = PropertiesUtils.getProperty("jpush_secret");
	private static final boolean IOS_APNS = Boolean.parseBoolean(PropertiesUtils.getProperty("jpush_ios_apns"));
	
	private static Logger logger = LoggerFactory.getLogger(JPushService.class);

	/**
	 * 推送
	 * 
	 * @param targets
	 *            推送目标别名集合
	 * @param content
	 *            推送的alert内容
	 * @param extras
	 *            需要传送给终端的附加参数
	 * @author: Tandong
	 * @date: 2017年12月22日 下午3:34:48
	 */
	public static void push(List<String> targets, Module module, String content, Map<String, String> extras) {
		JPushClient client = new JPushClient(SECRET, APP_KEY);
		PushPayload payload;

		try {
			Notification notifaction = Notification.newBuilder().setAlert(content)
					.addPlatformNotification(AndroidNotification.newBuilder().addExtra("module", module.toString())
							.addExtras(extras).build())
					.addPlatformNotification(IosNotification.newBuilder().addExtra("module", module.toString())
							.addExtras(extras).build())
					.build();

			payload = PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(targets))
					.setNotification(notifaction).setOptions(Options.newBuilder().setApnsProduction(IOS_APNS).build())
					.build();

			client.sendPush(payload);
		} catch (Exception e) {
			logger.error("推送失败~~~~~~~~", e);
		}
	}

	/**
	 * @Description: 跳转模块
	 * @author: Tandong
	 * @date: 2017年12月22日 下午4:30:48
	 * @Copyright: 2017 重庆网达科技  All rights reserved.
	 */
	public enum Module {
		FAULT_DETAIL
	}

	/**
	 * 本地测试方法
	 * @param args     
	 * @author: Tandong
	 * @date: 2017年12月22日 下午4:30:33
	 */
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("8486cb3be596487fb061a982c0195a03");

		Map<String, String> extras = new HashMap<String, String>();
		extras.put("id", "id");
		extras.put("a", "aaa");
		
		JPushService.push(list, Module.FAULT_DETAIL,  MessageFormat.format("设备{0}({1})出现故障！", "冰箱", "110111224"), extras);
	}

}
